package com.company.woye.hotmove;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.company.woye.Inventory.entity.BookBoxCallWMSEntity;
import com.company.woye.MyApplication;
import com.company.woye.R;
import com.company.woye.base.BaseActivity;
import com.company.woye.hotmove.adapter.HotMoveSearchAdapter;
import com.company.woye.hotmove.bean.HotMoveScan;
import com.company.woye.hotmove.bean.HotMoveSearch;
import com.company.woye.utils.BaseRequest;
import com.company.woye.utils.DBUtil;
import com.company.woye.utils.DataUtils;
import com.company.woye.utils.ScannerManager;
import com.company.woye.utils.SharedPreferenceUtils;
import com.company.woye.utils.Toaster;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import butterknife.BindView;
import butterknife.OnClick;

import static com.company.woye.utils.BaseRequest.postJsonData;

public class HotMoveActivity extends BaseActivity {

    @BindView(R.id.iv_on_package_back)
    ImageView ivOnPackageBack;
    @BindView(R.id.tv_on_package_back)
    TextView tvOnPackageBack;
    @BindView(R.id.tv_call)
    TextView tvCall;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_stop)
    TextView tvStop;
    @BindView(R.id.tv_quary)
    TextView tvQuary;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.tv_person)
    TextView tvPerson;
    @BindView(R.id.tv_HYH)
    TextView tvHYH;
    @BindView(R.id.tv_DBH)
    TextView tvDBH;
    @BindView(R.id.tv_HH)
    TextView tvHH;
    @BindView(R.id.tv_po)
    TextView tvPo;
    @BindView(R.id.tv_CPLX)
    TextView tvCPLX;
    @BindView(R.id.tv_PSSC)
    TextView tvPSSC;
    @BindView(R.id.tv_SC)
    TextView tvSC;
    @BindView(R.id.tv_SH)
    TextView tvSH;
    @BindView(R.id.tv_JS)
    TextView tvJS;
    @BindView(R.id.tv_CM)
    TextView tvCM;
    @BindView(R.id.tv_ZBS)
    TextView tvZBS;
    @BindView(R.id.tv_YSBS)
    TextView tvYSBS;
    @BindView(R.id.tv_ZJS)
    TextView tvZJS;
    @BindView(R.id.tv_YSJS)
    TextView tvYSJS;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.ed_order)
    EditText edOrder;
    @BindView(R.id.sp_part)
    Spinner spPart;
    @BindView(R.id.chkUnZyy)
    CheckBox chkUnZyy;
    @BindView(R.id.tv_clear)
    TextView tvClear;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_orderNo)
    TextView tvOrderNo;
    @BindView(R.id.tv_color)
    TextView tvColor;
    @BindView(R.id.tv_size)
    TextView tvSize;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_allbox)
    TextView tvAllbox;
    @BindView(R.id.tv_YYNum)
    TextView tvYYNum;
    @BindView(R.id.cb)
    TextView cb;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.rl)
    LinearLayout rl;
    @BindView(R.id.tv_boxs)
    TextView tvBoxs;
    @BindView(R.id.sp_color)
    Spinner spColor;
    @BindView(R.id.sp_size)
    Spinner spSize;
    private ScannerManager scannerManager;
    String OrderNo = "", Color = "", Size = "", curLoginUserNo = "", BoxNos = "", PartName = "";
    String curEmpNo = "", curEmpName = "";
    private final int MSG_ERROR = 9001;
    private final int MSG_SCAN = 9002;
    private final int MSG_SEARCH = 9003;
    private final int MSG_CALL = 9004;
    private final int MSG_START = 9005;
    private final int MSG_CallWMSResult = 9006;
    private final int MSG_CallWMSResult_CallBack = 9007;
    private final int MSG_CallAgvBoxOutInfo = 9008;
    private final int MSG_SEARCHPART = 9009;
    private final int MSG_SEARCHCOLOR = 9010;
    private final int MSG_SEARCHSIZE = 9011;
    private JSONArray scanJson, searchJson, callJson, stateJson, partJson,sizeJson,colorJson;
    HotMoveSearchAdapter searchAdapter;
    List<HotMoveSearch> searchList = new ArrayList<>();
    private String CurStationNo = "";
    private String TAG = getClass().getSimpleName();
    List<String> addBoxs = new ArrayList<>();
    private String TaskID = UUID.randomUUID().toString().toUpperCase();
    List<String> parts = new ArrayList<>();
    List<String> sizes = new ArrayList<>();
    List<String> colors = new ArrayList<>();
    ArrayAdapter<String> partAdapter,sizeAdapter,colorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_hot_move;
    }

    @Override
    public void initView() {
        CurStationNo = (String) SharedPreferenceUtils.getData(sharedPreferences, "leftGongWei", "");
        curLoginUserNo = MyApplication.curLoginUserNo;
        edCode.requestFocus();
        edCode.requestFocusFromTouch();
        //拦截扫描事件
        scannerManager = new ScannerManager(new ScannerManager.OnScanListener() {
            @Override
            public void onResult(String code, String deviceName) {
                edCode.setText(code);
                scanCode("SCAN", MSG_SCAN);
            }
        }, edCode.getContext());
        edCode.setInputType(InputType.TYPE_NULL);
        edCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edCode.setInputType(InputType.TYPE_CLASS_TEXT);
                //1.得到InputMethodManager对象
                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                //2.调用showSoftInput方法显示软键盘，其中view为聚焦的view组件
                imm.showSoftInput(edCode, InputMethodManager.SHOW_FORCED);
            }
        });
        edCode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event != null) {
                    Log.e("Debug", "-----------------------------Debug-----------------------------");
                    //System.out.println(event);
                    if ((event.getAction() == KeyEvent.ACTION_UP) && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                        //虚拟键盘或实体键盘可考虑是否通过deviceID，或source来判断
                        // KeyEvent:KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=0, metaState=0, flags=0x16, repeatCount=0, eventTime=142515176, downTime=142515176, deviceId=-1, source=0x0 }
                        //隐藏键盘
                        System.out.println(edCode.getText().toString());
                        scanCode("SCAN", MSG_SCAN);
                        return true;
                    }
                }
                return false;
            }
        });
        edCode.requestFocus();
        edCode.requestFocusFromTouch();
        searchAdapter = new HotMoveSearchAdapter(this, searchList);
        lv.setAdapter(searchAdapter);

        partAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, parts);
        spPart.setAdapter(partAdapter);
        sizeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, sizes);
        spSize.setAdapter(sizeAdapter);
        colorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, colors);
        spColor.setAdapter(colorAdapter);
        //合约号
        edOrder.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    scanCode("SEARCHPART", MSG_SEARCHPART);
                    return true;
                }
                return false;
            }
        });
    }

    private void scanCode(String OPERTYPE, int MSG) {
        hideIM(edCode);
        OrderNo = edOrder.getText().toString().trim();
        if (OPERTYPE.equals("SEARCH")){
            if (parts.size() > 0) {
                PartName = spPart.getSelectedItem().toString();
            }
            if (colors.size() > 0) {
                Color = spColor.getSelectedItem().toString();
            }
            if (sizes.size() > 0) {
                Size = spSize.getSelectedItem().toString();
            }
            if (addBoxs.size() > 0) {
                BoxNos = DataUtils.listToString(addBoxs);
            }
        }
        String tj = "";
        if (chkUnZyy.isChecked()) tj = ",@OnlyUnZyy=1";
        String sqlcmd = "Exec AGV_HotMove @OPERTYPE='" + OPERTYPE + "',@BarCode='" + edCode.getText().toString()
                + "',@OrderNo='" + OrderNo + "',@PartName='" + PartName + "',@Color='" + Color + "',@Size='" + Size
                + "',@UserNo='" + curLoginUserNo + "',@EmpNo='" + curEmpNo + "',@EmpName='" + curEmpName + "',@SiteNo='" + CurStationNo + "',@BoxNos='" + BoxNos + "'" + tj;
        ReadSQLData(sqlcmd, MSG);
    }

    @OnClick({R.id.tv_quary, R.id.tv_stop, R.id.tv_start, R.id.iv_on_package_back, R.id.tv_search, R.id.tv_call, R.id.tv_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_on_package_back:
            case R.id.iv_on_package_back:
                finish();
                break;
            //产量查询
            case R.id.tv_quary:
                Bundle bundle = new Bundle();
                bundle.putString("EmpNo", curEmpNo);
                bundle.putString("EmpName", curEmpName);
                bundle.putString("SiteNo", CurStationNo);
                Intent intent = new Intent(HotMoveActivity.this, QuaryQuantityActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.tv_clear:
                edOrder.setText("");
                break;
            case R.id.tv_search:
                if (parts.size() > 0) {
                    if (edOrder.getText().toString().length() > 0 || Color.length() > 0 || Size.length() > 0) {
                        scanCode("SEARCH", MSG_SEARCH);
                    } else {
                        Toast.makeText(HotMoveActivity.this, "至少输入一个条件进行查询", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HotMoveActivity.this, "没有部位信息", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_stop:
                scanCode("STOP", MSG_START);
                break;
            case R.id.tv_start:
                scanCode("START", MSG_START);
                break;
            case R.id.tv_call:
                addBoxs.clear();
                if (searchList != null && searchList.size() > 0) {
                    boolean isHave = false;
                    for (int i = 0; i < searchList.size(); i++) {
                        if (searchList.get(i).isCheck()) {
                            isHave = true;
                            addBoxs.add(searchList.get(i).getBoxNo());
                            System.out.println("searchList"+i+" "+searchList.get(i).toString());
                        }
                    }
                    if (isHave) {
                        callBoxOut(DataUtils.listToString(addBoxs));
                    } else {
                        Toast.makeText(HotMoveActivity.this, "至少选择一个进行呼叫", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HotMoveActivity.this, "呼叫列表为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    //改为API调用
    private void callBoxOut(String OutBoxNos) {
        TaskID = UUID.randomUUID().toString();
        String Sql = "Exec AGV_Stock_StockCallWMS @FormType='HotMove',@DepSiteNo='" + CurStationNo + "',@BoxType='出库'" +
                ",@TaskID='" + TaskID + "',@OperType='StockOutCallWMS',@UserNo='" + curLoginUserNo + "',@OutBoxNos='" + OutBoxNos + "'";
        ReadSQLData(Sql, MSG_CallAgvBoxOutInfo);
    }


    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm, int MSGS) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    JSONArray jsonArray;
                    System.out.println("过程:" + SQLComm);
                    jsonArray = DBUtil.QuerySQL2JSON(SQLComm);
                    System.out.println("结果" + jsonArray);
                    //往界面转数据;
                    if ((jsonArray != null) && (jsonArray.length() > 0)) {
                        //呼叫
                        if (jsonArray.optJSONObject(0).has("OperType") == true) {
                            //扫描
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("SCAN") == true) {
                                scanJson = jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSGS;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                            //查询
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("SEARCH") == true) {
                                searchJson = jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSGS;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                            //呼叫
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("StockOutCallWMS") == true) {
                                callJson = jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSGS;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                            //呼叫
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("START") == true
                                    || jsonArray.optJSONObject(0).getString("OperType").equals("STOP") == true) {
                                stateJson = jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSGS;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                            //查询部位
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("SEARCHPART") == true) {
                                partJson = jsonArray;
                                Message msg = new Message();
                                msg.what = MSGS;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                            //查询尺码
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("SEARCHSIZE") == true) {
                                sizeJson = jsonArray;
                                Message msg = new Message();
                                msg.what = MSGS;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                            //查询颜色
                            //查询尺码
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("SEARCHCOLOR") == true) {
                                colorJson = jsonArray;
                                Message msg = new Message();
                                msg.what = MSGS;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }

                        }
                        if (jsonArray.optJSONObject(0).has("SQLExcept") == true) {
                            //刷新界面;
                            Message errmsg = new Message();
                            Bundle bundle = new Bundle();
                            bundle.putString("HintInfo", jsonArray.optJSONObject(0).getString("SQLMessage"));
                            errmsg.what = MSG_ERROR;
                            errmsg.setData(bundle);
                            handler.removeMessages(errmsg.what);
                            handler.sendMessage(errmsg);
                        }
                    } else {
                        //刷新界面;
                        Message msg = new Message();
                        msg.what = MSGS;
                        handler.removeMessages(msg.what);
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    //刷新界面;
                    Message errmsg = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("HintInfo", e.getMessage());
                    errmsg.what = MSG_ERROR;
                    errmsg.setData(bundle);
                    handler.removeMessages(errmsg.what);
                    handler.sendMessage(errmsg);
                }

            }

        };
        new Thread(run).start();

    }

    //通过消息来执行处理类程序,不更新UI则不一定需要通过这个;
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // TODO Auto-generated method stub
            // 根据消息ID来判断要做的事情
            System.out.println("enter handleMessage");
            edCode.selectAll();
            edCode.requestFocus();
            edCode.requestFocusFromTouch();
            int msgId = msg.what;
            switch (msgId) {
                case MSG_ERROR:
                    Bundle bundle1 = msg.getData();
                    AlertDialog alertDialog1 = new AlertDialog.Builder(HotMoveActivity.this)
                            .setTitle(getString(R.string.app_name)).setMessage(bundle1.getString("HintInfo"))
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    return;
                                }
                            }).create();
                    alertDialog1.show();
                    break;
                case MSG_SCAN:
                    //扫描
                    if (scanJson != null && scanJson.length() > 0) {
                        try {
                            if (scanJson.getJSONObject(0).has("ResultInfo")) {
                                tvResult.setText(scanJson.getJSONObject(0).getString("ResultInfo"));
                            }
                            if (scanJson.getJSONObject(0).has("ErrInfo")) {
                                AlertDialog alertDialog = new AlertDialog.Builder(HotMoveActivity.this)
                                        .setTitle(getString(R.string.app_name)).setMessage(scanJson.getJSONObject(0).getString("ErrInfo"))
                                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                return;
                                            }
                                        }).create();
                                alertDialog.show();
                                clear();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        List<HotMoveScan> hotMoveScans = new Gson().fromJson(String.valueOf(scanJson), new TypeToken<List<HotMoveScan>>() {
                        }.getType());
                        if (hotMoveScans.get(0).getEmpNo() != null && hotMoveScans.get(0).getEmpNo().length() > 0) {
                            curEmpNo = hotMoveScans.get(0).getEmpNo();
                            curEmpName = hotMoveScans.get(0).getEmpName();
                            tvPerson.setText(curEmpNo + "-" + curEmpName);
                        } else {
                            tvPo.setText(hotMoveScans.get(0).getPO());
                            tvCM.setText(hotMoveScans.get(0).getCM());
                            tvCPLX.setText(hotMoveScans.get(0).getProductType());
                            tvDBH.setText(hotMoveScans.get(0).getDBH());
                            tvHH.setText(hotMoveScans.get(0).getGoodsNo());
                            tvHYH.setText(hotMoveScans.get(0).getOrderNo());
                            tvJS.setText(hotMoveScans.get(0).getJS());
                            tvPSSC.setText(hotMoveScans.get(0).getPSSC());
                            tvSC.setText(hotMoveScans.get(0).getSC());
                            tvSH.setText(hotMoveScans.get(0).getSH());
                            tvYSBS.setText(hotMoveScans.get(0).getYSBS());
                            tvYSJS.setText(hotMoveScans.get(0).getYSJS());
                            tvZBS.setText(hotMoveScans.get(0).getZBS());
                            tvZJS.setText(hotMoveScans.get(0).getZJS());
                            tvBoxs.setText("未做过的箱号:"+hotMoveScans.get(0).getNotUseBoxs());

                            //判断右侧合约号有没有  没有的话设置上合约号并获取部位 尺码 颜色  有合约号的话  不相等的时候触发获取部位 尺码 颜色  相等的话不触发 2022-02-22
                            if (!TextUtils.isEmpty(edOrder.getText())){
                                if (!TextUtils.isEmpty(tvHYH.getText().toString())){
                                    if (!tvHYH.getText().toString().equals(edOrder.getText().toString())){
                                        edOrder.setText(hotMoveScans.get(0).getOrderNo());
                                        scanCode("SEARCHPART", MSG_SEARCHPART);
                                    }
                                }
                            }else {
                                edOrder.setText(hotMoveScans.get(0).getOrderNo());
                                scanCode("SEARCHPART", MSG_SEARCHPART);
                            }

                        }
                    } else {
                        clear();
                    }
                    break;
                case MSG_SEARCH:
                    //查询
                    if (searchJson != null && searchJson.length() > 0) {
                        List<HotMoveSearch> hotMoveSearches = new Gson().fromJson(String.valueOf(searchJson), new TypeToken<List<HotMoveSearch>>() {
                        }.getType());
                        searchList.clear();
                        searchList.addAll(hotMoveSearches);
                        searchAdapter.notifyDataSetChanged();
                    } else {
                        searchList.clear();
                        searchAdapter.notifyDataSetChanged();
                    }
                    break;
                case MSG_SEARCHPART:
                    //部位
                    if (partJson != null && partJson.length() > 0) {
                        List<HotMoveSearch> partbeans = new Gson().fromJson(String.valueOf(partJson), new TypeToken<List<HotMoveSearch>>() {
                        }.getType());
                        parts.clear();
                        for (int i = 0; i < partbeans.size(); i++) {
                            parts.add(partbeans.get(i).getPartName());
                        }
                        partAdapter.notifyDataSetChanged();
                    } else {
                        parts.clear();
                        partAdapter.notifyDataSetChanged();
                        Toast.makeText(HotMoveActivity.this, "没有部位信息", Toast.LENGTH_SHORT).show();
                    }
                    scanCode("SEARCHCOLOR", MSG_SEARCHCOLOR);
                    break;
                    //查询尺寸
                case MSG_SEARCHSIZE:
                    if (sizeJson != null && sizeJson.length() > 0) {
                        List<HotMoveSearch> partbeans = new Gson().fromJson(String.valueOf(sizeJson), new TypeToken<List<HotMoveSearch>>() {
                        }.getType());
                        sizes.clear();
                        for (int i = 0; i < partbeans.size(); i++) {
                            sizes.add(partbeans.get(i).getSize());
                        }
                        sizeAdapter.notifyDataSetChanged();
                    } else {
                        sizes.clear();
                        sizeAdapter.notifyDataSetChanged();
                        Toast.makeText(HotMoveActivity.this, "没有尺寸信息", Toast.LENGTH_SHORT).show();
                    }
                    break;
                //查询颜色
                case MSG_SEARCHCOLOR:
                    if (colorJson != null && colorJson.length() > 0) {
                        List<HotMoveSearch> partbeans = new Gson().fromJson(String.valueOf(colorJson), new TypeToken<List<HotMoveSearch>>() {
                        }.getType());
                        colors.clear();
                        for (int i = 0; i < partbeans.size(); i++) {
                            colors.add(partbeans.get(i).getColor());
                        }
                        colorAdapter.notifyDataSetChanged();
                    } else {
                        colors.clear();
                        colorAdapter.notifyDataSetChanged();
                        Toast.makeText(HotMoveActivity.this, "没有颜色信息", Toast.LENGTH_SHORT).show();
                    }
                    scanCode("SEARCHSIZE", MSG_SEARCHSIZE);
                    break;
                //呼叫
                case MSG_CallWMSResult_CallBack:
                case MSG_CallAgvBoxOutInfo:
                    System.out.println("Enter MSG_CallAgvBoxOutInfo");
                    if (callJson != null && callJson.length() > 0) {
                        try {
                            if (callJson.getJSONObject(0).has("ResultInfo")) {
                                tvResult.setText(callJson.getJSONObject(0).getString("ResultInfo"));
                            }
                            if (callJson.getJSONObject(0).has("ErrInfo")) {
                                AlertDialog alertDialog = new AlertDialog.Builder(HotMoveActivity.this)
                                        .setTitle(getString(R.string.app_name)).setMessage(callJson.getJSONObject(0).getString("ErrInfo"))
                                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                return;
                                            }
                                        }).create();
                                alertDialog.show();
                                clear();
                            }
                            if (callJson.getJSONObject(0).has("CallWMS")) {
                                if (callJson.getJSONObject(0).getString("CallWMS").equals("Y")) {
                                    Gson gson = new Gson();
                                    List<BookBoxCallWMSEntity> bookBoxCallWMSEntities = gson.fromJson(String.valueOf(callJson), new TypeToken<List<BookBoxCallWMSEntity>>() {
                                    }.getType());
                                    //呼叫WMS
                                    bookBoxCallWMS(bookBoxCallWMSEntities);
                                }
                            }
                            if (callJson.getJSONObject(0).has("LoadDetail")) {
                                if (callJson.getJSONObject(0).getString("LoadDetail").equals("Y")) {
                                    BoxNos = "";
                                    addBoxs.clear();
                                    scanCode("SEARCH", MSG_SEARCH);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    //暂停  接收
                case MSG_START:
                    if (stateJson != null && stateJson.length() > 0) {
                        try {
                            if (stateJson.getJSONObject(0).has("ResultInfo")) {
                                tvResult.setText(stateJson.getJSONObject(0).getString("ResultInfo"));
                            }
                            if (stateJson.getJSONObject(0).has("ErrInfo")) {
                                AlertDialog alertDialog = new AlertDialog.Builder(HotMoveActivity.this)
                                        .setTitle(getString(R.string.app_name)).setMessage(stateJson.getJSONObject(0).getString("ErrInfo"))
                                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                return;
                                            }
                                        }).create();
                                alertDialog.show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case MSG_CallWMSResult:
                    System.out.println("MSG_CallWMSResult:" + MSG_CallWMSResult);
                    if (msg.arg1 == BaseRequest.HAND_REQUEST_FAILURE) {
                        System.out.println("MSG_CallWMSResult:" + 1);
                        Toaster.showLong(HotMoveActivity.this, "请求失败：" + msg.obj.toString());
                        return;
                    }
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        System.out.println("MSG_CallWMSResult:" + jsonObject.toString());
                        if (jsonObject.has("status") && String.valueOf(jsonObject.get("status")).equals("0")) {
                            Toaster.showLong(HotMoveActivity.this, "呼叫成功");
                        } else if (jsonObject.has("status") && jsonObject.get("status").equals("1")) {
                            Toaster.showLong(HotMoveActivity.this, "呼叫失败：" + jsonObject.get("errorinfo"));
                        } else {
                            if (jsonObject.has("errorinfo")) {
                                Toaster.showLong(HotMoveActivity.this, "呼叫失败：" + jsonObject.get("errorinfo"));
                            } else {
                                Toaster.showLong(HotMoveActivity.this, "呼叫失败");
                            }
                        }
                        System.out.println("MSG_CallWMSResult:" + 3);
                    } catch (JSONException e) {
                        Toaster.showLong(HotMoveActivity.this, "解析失败");
                    }
                    System.out.println("MSG_CallWMSResult:" + 4);
                    TransAgvCallInfo(msg.obj.toString(), "StockOutCallWMS");
                    System.out.println("MSG_CallWMSResult:" + 5);
                    break;
                default:
                    break;
            }
        }


        private void TransAgvCallInfo(String ReturnInfo, String operType) {
            System.out.println("Enter GetAgvCallInfo");
            String SqlComm = "Exec AGV_Check_CutInCallWMS_Trans @OperType='" + operType + "',@TaskID='" + TaskID + "',@ReturnInfo='" + ReturnInfo + "',@UserNo='" + MyApplication.curLoginUserNo + "'";
            Log.e(TAG, SqlComm);
            //调用WebService过程并处理界面更新;
            ReadSQLData(SqlComm, MSG_CallWMSResult_CallBack);
        }

        private void bookBoxCallWMS(List<BookBoxCallWMSEntity> bookBoxLists) {
            System.out.println("Enter bookBoxCallWMS");
            if (bookBoxLists != null && bookBoxLists.size() > 0) {
                String json = new Gson().toJson(bookBoxLists);
                if (MyApplication.UrlWms.isEmpty()) {
                    Toast.makeText(HotMoveActivity.this, "请先输入WMS地址", Toast.LENGTH_LONG).show();
                    return;
                }
                System.out.println(json);
                postJsonData(MyApplication.UrlWms + "/BookBoxOut", json, handler, MSG_CallWMSResult);
            }
        }

        public void clear() {
            tvPo.setText("");
            tvCM.setText("");
            tvCPLX.setText("");
            tvDBH.setText("");
            tvHH.setText("");
            tvHYH.setText("");
            tvJS.setText("");
            tvPSSC.setText("");
            tvSC.setText("");
            tvSH.setText("");
            tvYSBS.setText("");
            tvYSJS.setText("");
            tvZBS.setText("");
            tvZJS.setText("");
            tvBoxs.setText("未做过的箱号:");
        }
    };
}
